import Data.List
import System.IO
import System.Environment

main = do    
  
{-
    let (a, b) = take4 ([], [])  (words "1 + 3 * 4 + 9 - 5 / 2")
    print a
    print b

    let c = take5 a b
    print c
 -}
    let c = takeAll "1 + 3 * 4 + 9 - 5 / 3"
    print c

    print "******************:"

getOpPriority ::(Num a) => String-> a
getOpPriority x
    | x `elem` ["+","-"] = 1 
    | x `elem` [ "*", "/"] = 2 


calStr::(RealFloat a)=> a->a->String->a
calStr x y "+" = x + y
calStr x y "-" = x - y
calStr x y "*" = x * y
calStr x y "/" = x / y
calStr x y  op = x


takeAll ::(RealFloat a, Read a) => String-> a
takeAll str = take5 aa bb
    where  (aa, bb) = take4 ([], []) (words str)


take5 ::(RealFloat a) => [a]->[String]-> a
take5 (x:y:xs) (op1:ops) = take5 ((calStr y x op1):xs) ops
take5 (x:xs) ops = x


take4 ::(RealFloat a, Read a) => ([a], [String])->[String]->([a], [String])

take4 (digits, operators) [] = (digits, operators)

take4 (digit1:digits, op1:operators) (x:x2:xs)
    | x `elem` ["+","-", "*", "/"] = 
        if getOpPriority x >  getOpPriority op1 
            then  take4 (calStr digit1 (read x2) x : digits,  op1:operators)  (xs)
            else  take4 ( digit1:digits,  x:op1:operators)  (x2:xs)
    | otherwise =  take4 ( read x : digit1:digits, op1:operators)  (x2:xs)

take4 (digits, operators) (x:xs)
    | x `elem` ["+","-", "*", "/"] = take4 (digits,  (x:operators))  xs
    | otherwise =  take4 ( read x : digits, operators)  xs



